<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/5/12
 * Time: 9:43
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Matchset extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('boat')->count();// 获取总条数
            $lists = Db::name('boat')
                ->field('boat_id, boat_name, boat_long, boat_time, boat_thumb')
                ->page($Nowpage,$limits)
                ->order('boat_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加船型
     * @return mixed|\think\response\Json
     */
    public function boatAdd(){
        if(Request::instance()->isPost()){
            $boat_name = trim(input('boat_name'), ' ');
            $boat_long = trim(input('boat_long'), ' ');
            $boat_thumb = input('boat_thumb');
            $boat_sail_state = trim(input('boat_sail_state'), ' ');
            $boat_up_time = trim(input('boat_up_time'), ' ');
            $boat_cate = trim(input('boat_cate'), ' ');
            $boat_person_num = trim(input('boat_person_num'), ' ');
            $boat_weight = trim(input('boat_weight'), ' ');
            $boat_xuet = trim(input('boat_xuet'), ' ');
            $boat_caizhi = trim(input('boat_caizhi'), ' ');
            $boat_year = trim(input('boat_year'), ' ');
            $boat_width = trim(input('boat_width'), ' ');
            $boat_mianji = trim(input('boat_mianji'), ' ');
            $boat_qian_mianji = trim(input('boat_qian_mianji'), ' ');
            $boat_sanjiao_mianji = trim(input('boat_sanjiao_mianji'), ' ');
            $boat_yinf_mianji = trim(input('boat_yinf_mianji'), ' ');
            $boat_xuandiao = trim(input('boat_xuandiao'), ' ');
            $boat_chuanti_weight = trim(input('boat_chuanti_weight'), ' ');
            $boat_total_weight = trim(input('boat_total_weight'), ' ');
            $boat_paishui = trim(input('boat_paishui'), ' ');
            $boat_sheshui = trim(input('boat_sheshui'), ' ');
            $boat_designer = trim(input('boat_designer'), ' ');
            $insert = [
                'boat_name' => $boat_name,
                'boat_long' => $boat_long,
                'boat_thumb' => $boat_thumb,
                'boat_sail_state' => $boat_sail_state,
                'boat_up_time' => $boat_up_time,
                'boat_cate' => $boat_cate,
                'boat_person_num' => $boat_person_num,
                'boat_weight' => $boat_weight,
                'boat_xuet' => $boat_xuet,
                'boat_caizhi' => $boat_caizhi,
                'boat_year' => $boat_year,
                'boat_width' => $boat_width,
                'boat_mianji' => $boat_mianji,
                'boat_qian_mianji' => $boat_qian_mianji,
                'boat_sanjiao_mianji' => $boat_sanjiao_mianji,
                'boat_yinf_mianji' => $boat_yinf_mianji,
                'boat_xuandiao' => $boat_xuandiao,
                'boat_chuanti_weight' => $boat_chuanti_weight,
                'boat_total_weight' => $boat_total_weight,
                'boat_paishui' => $boat_paishui,
                'boat_sheshui' => $boat_sheshui,
                'boat_designer' => $boat_designer,
                'boat_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_boat')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('matchset/boatadd');
    }
    
    /**
     * 编辑船型
     * @return mixed|\think\response\Json
     */
    public function boatEdit(){
        $boat_id = input('boat_id');
        if(Request::instance()->isPost()){
            $boat_name = trim(input('boat_name'), ' ');
            $boat_long = input('boat_long');
            $boat_thumb = input('boat_thumb');
            $boat_sail_state = trim(input('boat_sail_state'), ' ');
            $boat_up_time = trim(input('boat_up_time'), ' ');
            $boat_cate = trim(input('boat_cate'), ' ');
            $boat_person_num = trim(input('boat_person_num'), ' ');
            $boat_weight = trim(input('boat_weight'), ' ');
            $boat_xuet = trim(input('boat_xuet'), ' ');
            $boat_caizhi = trim(input('boat_caizhi'), ' ');
            $boat_year = trim(input('boat_year'), ' ');
            $boat_width = trim(input('boat_width'), ' ');
            $boat_mianji = trim(input('boat_mianji'), ' ');
            $boat_qian_mianji = trim(input('boat_qian_mianji'), ' ');
            $boat_sanjiao_mianji = trim(input('boat_sanjiao_mianji'), ' ');
            $boat_yinf_mianji = trim(input('boat_yinf_mianji'), ' ');
            $boat_xuandiao = trim(input('boat_xuandiao'), ' ');
            $boat_chuanti_weight = trim(input('boat_chuanti_weight'), ' ');
            $boat_total_weight = trim(input('boat_total_weight'), ' ');
            $boat_paishui = trim(input('boat_paishui'), ' ');
            $boat_sheshui = trim(input('boat_sheshui'), ' ');
            $boat_designer = trim(input('boat_designer'), ' ');
            $update = [
                'boat_name' => $boat_name,
                'boat_long' => $boat_long,
                'boat_sail_state' => $boat_sail_state,
                'boat_up_time' => $boat_up_time,
                'boat_cate' => $boat_cate,
                'boat_person_num' => $boat_person_num,
                'boat_weight' => $boat_weight,
                'boat_xuet' => $boat_xuet,
                'boat_caizhi' => $boat_caizhi,
                'boat_year' => $boat_year,
                'boat_width' => $boat_width,
                'boat_mianji' => $boat_mianji,
                'boat_qian_mianji' => $boat_qian_mianji,
                'boat_sanjiao_mianji' => $boat_sanjiao_mianji,
                'boat_yinf_mianji' => $boat_yinf_mianji,
                'boat_xuandiao' => $boat_xuandiao,
                'boat_chuanti_weight' => $boat_chuanti_weight,
                'boat_total_weight' => $boat_total_weight,
                'boat_paishui' => $boat_paishui,
                'boat_sheshui' => $boat_sheshui,
                'boat_designer' => $boat_designer
            ];
            if(!empty($boat_thumb)){
                $update['boat_thumb'] = $boat_thumb;
            }
            $res = Db::table('up_boat')->where('boat_id', $boat_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $boat = Db::table('up_boat')
            ->where('boat_id', $boat_id)
            ->field('boat_name, boat_long, boat_thumb, boat_sail_state, boat_up_time, boat_cate, 
            boat_person_num, boat_weight, boat_xuet, boat_caizhi, boat_year, boat_width, boat_mianji, 
            boat_qian_mianji, boat_sanjiao_mianji, boat_yinf_mianji, boat_xuandiao, boat_chuanti_weight, 
            boat_total_weight, boat_paishui, boat_sheshui, boat_designer')
            ->find();
        return $this->fetch('matchset/boatedit', ['boat' => $boat, 'boat_id' => $boat_id]);
    }
    
    /**
     * 删除船型
     * @return \think\response\Json
     */
    public function boatDel(){
        $boat_id = input('id');
        $res = Db::table('up_boat')->delete($boat_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}